package net.atomarrow.action;

import com.alibaba.fastjson.JSONObject;
import net.atomarrow.util.RequestUtil;
import net.atomarrow.util.XssFilterUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;

/**
 * @author Boolone
 * @date 2020/2/14
 */
public class AtomarrowRequest extends HttpServletRequestWrapper {

    private static final String JSON_CONTENT_TYPE = "application/json";

    private HttpServletRequest request;

    private Action action;

    private boolean isJSONRequest; //是否是json请求

    private JSONObject jsonData; //如果是json请求，将json字符串转成json对象

    public AtomarrowRequest(HttpServletRequest request, Action action) {

        super(request);

        this.request = request;

        this.action = action;

        String contentType = request.getContentType();

        isJSONRequest = contentType != null && contentType.contains(JSON_CONTENT_TYPE);

        if (isJSONRequest) {

            jsonData = RequestUtil.getRequestBodyAsJSON(request);

            if (jsonData == null) {
                jsonData = new JSONObject();
            }
        }

    }

    /**
     * 获取通过url或者formdata传入的所有参数,
     * 通过json传入的参数，String[]中只可以获得value值的json格式字符串
     * @return
     */
    @Override
    public Map<String, String[]> getParameterMap() {

        if (isJSONRequest) {
            HashMap<String, String[]> map = new HashMap();
            for (String key : jsonData.keySet()) {
                map.put(key, new String[]{XssFilterUtil.stripXss(jsonData.get(key).toString())});
            }
            return map;
        }


        Map<String, String[]> map = new HashMap<>(request.getParameterMap());
        Iterator<Map.Entry<String, String[]>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String[]> entry = it.next();
            String[] values = entry.getValue();
            if (values == null) continue;
            for(int i=0; i<values.length; i++){
                values[i] = XssFilterUtil.stripXss(values[i]);
            }
            map.put(entry.getKey(), values);
        }

        return map;
    }


    @Override
    public String getParameter(String s) {
        String value = null;
        if (isJSONRequest) {
            Object v = jsonData.get(s);
            value = v != null ? v.toString() : null;
        }
        if (value == null) {
            value = request.getParameter(s);
        }
        return XssFilterUtil.stripXss(value);
    }


    @Override
    public Enumeration<String> getParameterNames() {
        if (isJSONRequest) {
            return Collections.enumeration(jsonData.keySet());
        }
        return request.getParameterNames();
    }

    @Override
    public String[] getParameterValues(String s) {
        String[] values = null;
        if (isJSONRequest) {
            values= new String[]{jsonData.get(s).toString()};
        } else {
            values = request.getParameterValues(s);
        }
        if (values == null) {
            return null;
        }
        int count = values.length;
        String[] encodedValues = new String[count];
        for (int i = 0; i < count; i++) {
            encodedValues[i] = XssFilterUtil.stripXss(values[i]);
        }
        return encodedValues;


    }


    /**
     * 返回原生HttpServletRequest
     * @return
     */
    public HttpServletRequest getOriginRequest() {
        return request;
    }

    public boolean isJSONRequest() {
        return isJSONRequest;
    }

    public JSONObject getJsonData() {
        return jsonData;
    }

    public Action getAction() {
        return action;
    }



}
